
#import "WaterFlowLayout.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface WaterFlowLayout()
// 获取item的总数量
@property (nonatomic,assign) NSUInteger numberOfItems;

// 用来保存每一列的高度
@property (nonatomic,retain) NSMutableArray *arrayForColumnHeights;

//用来保存每一个item计算好的属性（x,y,w,h）
@property (nonatomic,strong) NSMutableArray *arrayForItemAtrributes;

// 保存每个item的X值
@property (nonatomic, assign) CGFloat XForItemOrigin;
// 保存每个item的Y值
@property (nonatomic, assign) CGFloat YForItemOrigin;
// 纪录最短的列
@property (nonatomic, assign) NSInteger shortestIndex;

@property (nonatomic, retain) NSMutableArray *arrayHeader;



@end

@implementation WaterFlowLayout


- (void)dealloc {
    
     [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [super dealloc];
    [_arrayForColumnHeights release];
    
    [_arrayForItemAtrributes release];
    
    [_arrayHeader release];
    
    [_number release];
    
}


#pragma mark  - collectionViewLayout类方法.

// 准备布局
- (void)prepareLayout {
    
  
    
    // 调用父类布局
    [super prepareLayout];
    
    // 初始化
  //  NSNumber *number = [NSNumber numberWithFloat:HEIGHT];
    
    self.arrayForColumnHeights = @[self.number,self.number].mutableCopy;
    
  
   
 //   self.arrayForItemAtrributes = [NSMutableArray array];
    
    [self attrsArray];
    [self.arrayForItemAtrributes removeAllObjects];
    
    
    // 获取item的数量
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
    
    // 为每一个item确定LayoutAttribute属性,同时将这些属性放入布局数组中.  frame和indexPath
    for(int i = 0;i < self.numberOfItems;i++)
    {
        
        // 确定每个Item的origin的X,Y值.
        [self getItemOriginPoint];
        
        // 确定每个Item的indexPath属性.同时确定了每个Item的LayoutAttribute.放入到了布局属性数组中.
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        [self setFrame:indexPath];
    }
    


  //  [self.arrayForColumnHeights release];
    
}

- (NSMutableArray *)attrsArray{
    if (self.arrayForItemAtrributes == nil) {
        self.arrayForItemAtrributes = [NSMutableArray array];
    }
    return self.arrayForItemAtrributes;
}


// 返回每一个item的attribute
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
       // 返回每一个item的Attribute
    return self.arrayForItemAtrributes;
    
}


// 计算contentView的大小
- (CGSize)collectionViewContentSize {
    
    
    // 获取最长高度索引
    NSInteger longerstIndex = [self p_indexForLongestColumn];
    
    // 通过索引获取高度
    CGFloat longestH = [self.arrayForColumnHeights[longerstIndex] floatValue];
    
    // 获取collectionView的Size
    CGSize contentSize = self.collectionView.frame.size;
    
    // 最大高度+bottom
    contentSize.height = longestH + 10;
    
    NSNumber *number = [NSNumber numberWithDouble:contentSize.height];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"height" object:nil userInfo:@{@"height":number}];
    
   
    
    return contentSize;
}

#pragma mark - feature

// 获取最长列的索引
- (NSInteger)p_indexForLongestColumn {
    // 记录那一列最长
    NSInteger longestIndex = 0;
    // 记录当前最长高度
    CGFloat longestHeight = 0;
    for(int i = 0;i < 2;i++)
    {
        // 获取高度
        CGFloat currentHeight = [self.arrayForColumnHeights[i] floatValue];
        // 判断，选出最长高度
        if (currentHeight > longestHeight) {
            longestHeight = currentHeight;
            longestIndex = i;
        }
    }
    return longestIndex;
}


// 查找最短的列，并设置相关属性
- (void)getItemOriginPoint {
    
    // 记录最小高度的索引值.
    NSInteger shortestIndex = 0;
    
    // 记录最小值
    CGFloat shortestHeight = MAXFLOAT;
    for (int i = 0; i < 2; i++) {
        
        // 获取当前高度
        CGFloat currentHeight = [self.arrayForColumnHeights[i] floatValue];
        if (currentHeight < shortestHeight) {
            shortestHeight = currentHeight;
            shortestIndex = i;
        }
    }
    self.shortestIndex = shortestIndex;
    
    // 计算x值：内边距left + （item宽 + item的间距）* 索引
    self.XForItemOrigin = 5 + (self.itemSize.width + self.insertItemSpacing) * _shortestIndex;
    
    // 计算y值
    self.YForItemOrigin = [self.arrayForColumnHeights[_shortestIndex] floatValue] + self.insertItemSpacing ;
    
}

// 设置属性和frame
- (void)setFrame:(NSIndexPath *)indexPath {
    
    // 设置Item layoutAttribute 属性
    UICollectionViewLayoutAttributes *layoutArr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    // 获取 item 的高
    
    CGFloat itemHeight = 1400;
    
    if (_delegate && [_delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
        
        // 使用代理方法获取item的高
        itemHeight = [_delegate heightForItemIndexPath:indexPath];
    }
    
    // 设置frame
    layoutArr.frame = CGRectMake(_XForItemOrigin, _YForItemOrigin, self.itemSize.width, itemHeight);
    
    // 放入数组
    [self.arrayForItemAtrributes addObject:layoutArr];
    
    // 更新高度
    
    self.arrayForColumnHeights[_shortestIndex] = @(_YForItemOrigin + itemHeight);
}




@end
